const postcss = require("postcss")
const { generateRandomString } = require("./utils.js")

const plugin = () => ({
  postcssPlugin: "postcss-reverse-props",
  Once(root) {
    root.walkRules((rule) => {
      rule.selector = `.${generateRandomString()}`
    })
  },
})
plugin.postcss = true

const transformCss = (cssCode) =>
  postcss([plugin])
    .process(cssCode, {
      from: undefined,
      to: undefined,
    })
    .then((res) => res)
    .catch(() => Promise.resolve({ css: cssCode }))

const transformCssGroup = (list) => {
  const group = list.map((item) => transformCss(item))
  return Promise.all(group)
}
module.exports = { transformCss, transformCssGroup }
